// rpcServerService project rpcServerService.go
package rpcServerService

import (
	"fmt"
	"net"
	"net/rpc"
	"net/rpc/jsonrpc"
	"os"
)

func NewRpcServerService() *RpcServerService {
	return &RpcServerService{}
}

type RpcServerService struct {
}

func (p *RpcServerService) StartMonitor() {
	dataService := new(DataService)
	rpc.Register(dataService)
	tcpAddr, err := net.ResolveTCPAddr("tcp", "192.168.2.13:54323")
	p.checkError(err)

	listener, err := net.ListenTCP("tcp", tcpAddr)
	p.checkError(err)
	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		go jsonrpc.ServeConn(conn)
	}
}

func (p *RpcServerService) checkError(err error) {
	if err != nil {
		fmt.Println("Fatal error ", err.Error())
		os.Exit(1)
	}
}
